import api from '@/api/index'

// 辅助函数：使用默认板块数据
const useDefaultBoardData = (commit, resolve) => {
  const topBoards = [
    { id: 1, name: '综合讨论', description: '讨论各种话题', articleCount: 128, todayCount: 12 },
    { id: 2, name: '技术分享', description: '分享技术心得与经验', articleCount: 96, todayCount: 8 },
    { id: 3, name: '生活娱乐', description: '分享生活中的趣事', articleCount: 75, todayCount: 5 }
  ];
  console.log('使用默认板块数据:', topBoards);
  commit('SET_TOP_BOARDS', topBoards);
  resolve(topBoards);
};

// 辅助函数：使用默认所有板块数据
const useDefaultAllBoardsData = (commit, resolve) => {
  const allBoards = [
    { id: 1, name: '综合讨论', description: '讨论各种话题', articleCount: 128, todayCount: 12 },
    { id: 2, name: '技术分享', description: '分享技术心得与经验', articleCount: 96, todayCount: 8 },
    { id: 3, name: '生活娱乐', description: '分享生活中的趣事', articleCount: 75, todayCount: 5 },
    { id: 4, name: '问答社区', description: '解答各种问题', articleCount: 64, todayCount: 7 },
    { id: 5, name: '资源分享', description: '分享有用的资源', articleCount: 42, todayCount: 3 }
  ];
  console.log('使用默认所有板块数据:', allBoards);
  commit('SET_ALL_BOARDS', allBoards);
  resolve(allBoards);
};

const state = {
  topBoards: [],
  allBoards: [],
  isLoading: false
}

const mutations = {
  SET_LOADING: (state, isLoading) => {
    state.loading = isLoading
  },
  SET_TOP_BOARDS: (state, boards) => {
    state.topBoards = boards
  },
  SET_ALL_BOARDS: (state, boards) => {
    state.allBoards = boards
  },
  SET_BOARDS: (state, boards) => {
    state.allBoards = boards
  },
  SET_CURRENT_BOARD: (state, board) => {
    state.currentBoard = board
  }
}

const actions = {
  // 获取置顶板块
  async getTopBoards({ commit }) {
    return new Promise((resolve) => {
      try {
        api.board.getTopBoards().then(response => {
          if (response && response.code === 0) {
            const boards = response.data || []
            commit('SET_TOP_BOARDS', boards)
            resolve(boards)
          } else {
            // 当API返回非成功状态码时，使用默认数据
            useDefaultBoardData(commit, resolve)
          }
        }).catch(() => {
          // 当API请求失败时，使用默认数据
          useDefaultBoardData(commit, resolve)
        })
      } catch (error) {
        // 静默处理错误，使用默认数据
        useDefaultBoardData(commit, resolve)
      }
    })
  },
  
  // 获取所有板块
  async getAllBoards({ commit }) {
    return new Promise((resolve) => {
      try {
        api.board.getAllBoards().then(response => {
          if (response && response.code === 0) {
            const boards = response.data || []
            commit('SET_ALL_BOARDS', boards)
            resolve(boards)
          } else {
            // 当API返回非成功状态码时，使用默认数据
            useDefaultAllBoardsData(commit, resolve)
          }
        }).catch(() => {
          // 当API请求失败时，使用默认数据
          useDefaultAllBoardsData(commit, resolve)
        })
      } catch (error) {
        // 静默处理错误，使用默认数据
        useDefaultAllBoardsData(commit, resolve)
      }
    })
  }
}

const getters = {
  topBoards: state => state.topBoards,
  allBoards: state => state.allBoards,
  isLoading: state => state.isLoading,
  getBoardById: state => id => {
    const board = state.allBoards.find(b => b.id === id)
    if (board) return board
    return state.topBoards.find(b => b.id === id)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 